<template>
    <div>
        <h1>文件管理</h1>

        <!-- 搜索框 -->
        <div style="margin: 10px 0;">
            <el-input v-model="nameSearch" placeholder="请输入文件名" style="width: 220px;" prefix-icon="el-icon-search"
                clearable></el-input>
            <el-button type="primary" style="margin-left: 5px;" @click="load">搜索</el-button>
            <el-button style="margin-left: 5px;" @click="resetInput">重置</el-button>
        </div>

        <!-- 功能按钮 -->
        <div style="margin: 10px 0;">
            <el-upload action="http://localhost:9090/file/upload" :show-file-list="false" style="display: inline-block;"
            :on-success="handleFileUploadSuccess">
                <el-button type="success">文件上传 <i class="el-icon-folder-add"></i></el-button>
            </el-upload>
            <el-popconfirm style="margin-left: 10px;" confirm-button-text='好的' cancel-button-text='不用了'
                icon="el-icon-info" icon-color="red" title="您确定批量删除这些数据吗？" @confirm="handleDelBatch">
                <el-button type="danger" slot="reference">批量删除 <i class="el-icon-remove-outline"></i></el-button>
            </el-popconfirm>
        </div>

        <!-- 表格 -->
        <el-table :data="tableData" border stripe height="440" header-cell-style="background: #F3F3F3;"
            @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column prop="id" label="ID" width="70" fixed="left" sortable>
            </el-table-column>
            <el-table-column prop="name" label="文件名称" width="300">
            </el-table-column>
            <el-table-column prop="type" label="文件类型">
            </el-table-column>
            <el-table-column prop="size" label="文件大小(KB)">
            </el-table-column>
            <el-table-column prop="url" label="下载">
                <template slot-scope="scope">
                    <el-button type="primary" @click="download(scope.row.url)" style="width: 60px; font-size: 15px;"><i class="el-icon-download"></i></el-button>
                </template>
            </el-table-column>
            <el-table-column label="状态">
                <template slot-scope="scope">
                    <el-switch v-model="scope.row.enable" @change="changeEnable(scope.row)" active-color="#13ce66" inactive-color="#ccc"></el-switch>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="180" fixed="right">
                <template slot-scope="scope">
                    <el-popconfirm style="margin-left: 8px;" confirm-button-text='好的' cancel-button-text='不用了'
                        icon="el-icon-info" icon-color="red" title="您确定删除吗？" @confirm="handleDelete(scope.row.id)">
                        <el-button type="danger" slot="reference">删除 <i class="el-icon-delete"></i>
                        </el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页区域 -->
        <div style="padding: 10px 0;">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageNum"
                :page-sizes="[5, 10, 15, 20]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>
    </div>
</template>

<script>
    export default {
        name: "File",
        data() {
            return {
                tableData: [],
                total: 0,
                pageNum: 1,
                pageSize: 10,
                nameSearch: '',
                multipleSelection: [],

            }
        },
        created() {
            this.load()
        },
        methods: {
            load() {
                this.request.get("/file/page", {
                    params: {
                        pageNum: this.pageNum,
                        pageSize: this.pageSize,
                        name: this.nameSearch,
                    }
                }).then(res => {
                    console.log(res)
                    this.tableData = res.data.records
                    this.total = res.data.total
                })
            },
            // 获取每页显示几条的数据
            handleSizeChange(pageSize) {
                // console.log(pageSize)
                this.pageSize = pageSize
                this.load()
            },
            // 获取第某个页从第几个开始显示的数据
            handleCurrentChange(pageNum) {
                // console.log(pageNum);
                this.pageNum = pageNum
                this.load()
            },
            // 搜索框重置按钮
            resetInput() {
                this.nameSearch = ""
                this.load()
            },
            // 下载按钮
            download(url) {
                window.open(url)
            },
            // 删除按钮
            handleDelete(id) {
                this.request.delete("/file/" + id).then(res => {
                    if (res.code === '200') {
                        this.$message.success("删除成功")
                        this.load()
                    } else {
                        this.$message.error("删除失败")
                    }
                })
            },
            // 表格批量选择
            handleSelectionChange(val) {
                console.log(val)
                this.multipleSelection = val
            },
            // 批量删除按钮
            handleDelBatch() {
                let ids = this.multipleSelection.map(v => v.id) // {{}, {}, {}} => [1,2,3]
                this.request.post("/file/del/batch", ids).then(res => {
                    if (res.code === '200') {
                        this.$message.success("批量删除成功")
                        this.load()
                    } else {
                        this.$message.error("批量删除失败")
                    }
                })
            },
            // 上传文件
            handleFileUploadSuccess(res) {
                console.log(res);
                this.load()
            },
            changeEnable(row) {
                this.request.post("/file/update", row).then(res => {
                    if (res.code === '200') {
                        this.$message.success("操作成功")
                    }
                })
            },
        }
    }
</script>

<style scoped>

</style>